(function () {
    angular.module('app')
        .component('xxUpdateStock',{
            templateUrl:'/app/drug-manager/update-stock/update-stock.html',
            controller:ctrl
        });

    function ctrl($scope,$state,$stateParams,$mdDialog,userService,mockHttp,UpdateStockFactory) {
        $scope.drug = mockHttp.allDrug['res'][0];
        $scope.shopId = userService.user['shopId'];
        $scope.drugId = $stateParams.drugId;
        $scope.updateNumber = 0;
        $scope.status = '';

        /*function getStockInfoById(shopId, drugId) {
            function cb(err, res) {
                if (!err) console.log(err, res)
            }
            UpdateStockFactory.getStockInfoById(shopId, drugId, cb);
        }
        getStockInfoById($scope.drug['drugId'], $scope.shopId);*/

        $scope.showConfirm = function (){
            // Appending dialog to document.body to cover sidenav in docs app
            var confirm = $mdDialog.confirm()
                .title('确认更新？')
                .textContent('更新后不可更改')
                .ariaLabel('Lucky day')
                // .targetEvent(ev)
                .ok('确认')
                .cancel('取消');

            $mdDialog.show(confirm).then(function() {
                $scope.status = '更新成功！'+ $scope.updateNumber;
                function updateStock (shopId, drugId, updateNumber) {
                    function cb(err, res) {
                        if (!err) {
                            $scope.drug = res['res'][0];
                            console.log(res['res']);
                        }
                    }
                    UpdateStockFactory.updateStock(shopId, drugId, updateNumber, cb);
                }
                updateStock ($scope.shopId, $scope.drugId, $scope.updateNumber);

            }, function() {
                $scope.status = '您取消了更新';
            });
        };

    }
})();